package com.hl.icd.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.hl.icd.bean.ICD;
import com.hl.icd.bean.Mapping;
import com.hl.icd.service.MapperService;
import com.hl.icd.utils.ExcelUtil;
import com.hl.icd.utils.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Api(tags = "编码映射接口管理文档")
public class MapperController {

    @Autowired
    private MapperService mapperService;

    @Autowired
    private ExcelUtil excelUtil;

    @ApiOperation(
            value = "获取编码映射列表",
            notes = "根据页数和每页的数量分页获取编码映射列表",
            httpMethod = "GET",
            response = String.class)
    @GetMapping(value = "/mapperQuery/{pageNum}/{pageSize}",produces="text/html;charset=UTF-8")
    public String mapperQuery(@PathVariable int pageNum,
                              @PathVariable int pageSize,
                              Mapping mapping){
        String json = "";
        ObjectMapper om = new ObjectMapper();
        List<Mapping> mappings = mapperService.mapperQuery(pageNum,pageSize,mapping);
        Map<String,Object> resMap = new HashMap<>();
        PageInfo pageInfo = new PageInfo(mappings);
        String total = String.valueOf(pageInfo.getTotal());
        resMap.put("total",total);
        resMap.put("data",mappings);
        try {
            json = om.writeValueAsString(resMap);
            System.out.println(json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }

    @ApiOperation(
            value = "添加编码映射功能",
            notes = "根据前端提供的编码映射关系进行编码映射添加操作，此方法一般用在管理员对编码映射表的操作",
            httpMethod = "POST",
            response = String.class)
    @PostMapping(value = "/mapper/save",produces="text/html;charset=UTF-8")
    public String MapperSave(@RequestBody Mapping mapping){
        return mapperService.insertOne(mapping);
    }

    @ApiOperation(
            value = "删除一条编码映射数据",
            notes = "根据前端提供的编码映射id进行编码映射删除操作，此方法一般用在管理员对编码映射表的操作",
            httpMethod = "DELETE",
            response = String.class)
    @DeleteMapping(value = "/mapper/deleteOne/{id}")
    public String deleteOne(@PathVariable String id){
        return mapperService.deleteOneById(id);
    }

    @ApiOperation(
            value = "更新编码编码信息功能",
            notes = "根据前端提供的编码映射信息进行编码映射更新操作，此方法一般用在管理员对编码映射表的操作",
            httpMethod = "PUT",
            response = String.class)
    @PutMapping(value = "/mapper/update")
    public String mapperUpdate(@RequestBody Mapping mapping){
        return mapperService.updateOne(mapping);
    }

    @ApiOperation(
            value = "批量删除编码映射功能",
            notes = "根据前端提供的编码映射id集合进行编码映射的批量删除操作，此方法一般用在管理员对编码映射表的操作",
            httpMethod = "DELETE",
            response = String.class)
    @DeleteMapping(value = "/mapper/deleteBatch")
    public String deleteBatch(@RequestBody List<String> ids){
        return mapperService.deleteBatch(ids);
    }

    @RequestMapping("/mapper/upload")
    public void uploadFile(MultipartFile file, HttpServletResponse response) throws Exception {
        //解析excel文件
        List<List<Object>> rows = excelUtil.getBankListByExcel(file.getInputStream(),file.getOriginalFilename());
        Mapping mapping = new Mapping();
        for (List<Object> row : rows) {
            /*icd.setId(UUIDUtil.getUUID());
            icd.setIcdCode((String) row.get(0));
            icd.setName((String) row.get(1));
            icd.setType((String) row.get(2));*/
            mapping.setId(UUIDUtil.getUUID());
            mapping.setIcdXCode((String) row.get(0));
            mapping.setIcdXDesc((String) row.get(1));
            mapping.setIcdYCode((String) row.get(2));
            mapping.setIcdYDesc((String) row.get(3));
            mapping.setIcdYName((String) row.get(4));
            mapping.setMappingTypeText((String) row.get(5));
            mapping.setExpressionDegreeText((String) row.get(6));
            mapping.setxVid((String) row.get(7));
            mapping.setyVid((String) row.get(8));
            mapperService.insertOne(mapping);
        }
    }



}
